Skip to main content
Glama
ayupow

LNR-server-01-input-data-processing

schedule_restoration_sequence_considering_closeness.py2.37 kB
import networkx as nx import json def schedule_restoration_sequence_considering_closeness(global_json_path): # Read global data file with open(global_json_path, 'r') as f: file_paths = json.load(f) # Read network topology file with open(file_paths['interdependent_infrastructure_networks'], 'r') as f: network_topology = json.load(f) # Read closeness centrality file with open(file_paths['facility_importance_using_closeness_centrality'], 'r') as f: centrality_data = json.load(f) # Read failure nodes file with open(file_paths['cascading_failure_information'], 'r') as f: failure_data = json.load(f) # Construct graph G = nx.Graph() for edge in network_topology['edges']: G.add_edge(edge['Start'], edge['End'], infrastructure_type=edge['Infrastructure Type']) # Assign centrality values to nodes for node in centrality_data['nodes']: G.nodes[node['Code']]['closeness_centrality'] = node['closeness'] # Select only failed nodes for recovery failed_nodes = failure_data['failed_nodes'] # Sort failed nodes by closeness centrality sorted_failed_nodes = sorted( [node for node in G.nodes(data=True) if node[0] in failed_nodes], key=lambda x: x[1]['closeness_centrality'], reverse=True ) # Sort edges related to failed nodes sorted_failed_edges = sorted( [(u, v) for u, v in G.edges() if u in failed_nodes or v in failed_nodes], key=lambda x: (G.nodes[x[0]]['closeness_centrality'] + G.nodes[x[1]]['closeness_centrality']), reverse=True ) # Generate recovery order recovery_order = { "recovery_order": { "nodes": [node[0] for node in sorted_failed_nodes], "edges": [{"Start": edge[0], "End": edge[1]} for edge in sorted_failed_edges] } } # Output file path output_json_path = 'recovery_strategy_of_closeness_centrality.json' # Save recovery order to output file with open(output_json_path, 'w') as f: json.dump(recovery_order, f, indent=4) # Update global data file path file_paths['recovery_strategy_of_closeness_centrality'] = output_json_path with open(global_json_path, 'w') as f: json.dump(file_paths, f, indent=4)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ayupow/LNR-server-01-input-data-processing'

If you have feedback or need assistance with the MCP directory API, please join our Discord server